package com.restaurant.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.restaurant.domain.Employeecredential;
import com.restaurant.validators.LoginFormValidator;

@Component
@RequestMapping("/")
public class LoginFormController {
	
//	private LoginFormValidator loginFormValidator;
//	
//	@Autowired
//	public void setLoginFormValidator(LoginFormValidator loginFormValidator){
//		this.loginFormValidator = loginFormValidator;
//	}
	
//	@RequestMapping(method = RequestMethod.POST)
//	public String verifyUserCredential(@ModelAttribute Employeecredential employeecredential, BindingResult results, HttpServletRequest request, HttpServletResponse response){
//		loginFormValidator.validate(employeecredential, results);
//		if(results.hasErrors())
//			return "login";
//		return "home";		
//	}
	
	@RequestMapping(value="/login",method = RequestMethod.GET)
	public String login(Model map){//, ModelMap modelMap){
//		HttpSession  session = request.getSession(true);
//		session.setAttribute("name", "anuj");
//		Employeecredential employeecredential = new Employeecredential();
//		employeecredential.setUsername("anuj");
//		map.addAttribute("employeecredential", employeecredential);
		return "login";
	}
	
	@RequestMapping(value="/loginfailed", method = RequestMethod.GET)
	public String loginFailed(Model map){//, ModelMap modelMap){
		map.addAttribute("error", "true");
		return "login";
	}
	
	@Secured("ROLE_ADMIN")
	@RequestMapping(value = "/action", method = RequestMethod.GET)
	public String home(Model map){//, ModelMap modelMap){
		return "home";
	}
}
